package top.lyc829.iot_android.fragment.profile.finger;

import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.model.ApiResult;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;

import butterknife.BindView;
import top.lyc829.iot_android.R;
import top.lyc829.iot_android.core.BaseFragment;
import top.lyc829.iot_android.entity.Finger;
import top.lyc829.iot_android.entity.User;
import top.lyc829.iot_android.http.service.UserService;
import top.lyc829.iot_android.http.subscriber.TipProgressLoadingSubscriber;
import top.lyc829.iot_android.util.Utils;

@Page(name = "EditFingerFragment")
public class EditFingerFragment extends BaseFragment {

    Finger finger;
    int position = -1;

    boolean isAdd = false;

    @BindView(R.id.et_finger_name)
    EditText etFingerName;

    @BindView(R.id.btn_delete_finger)
    SuperButton btnDeleteFinger;

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setTitle("指纹");
        titleBar.addAction(new TitleBar.TextAction("完成") {
            @Override
            public void performAction(View view) {
                if (isAdd) {
                    addFinger();
                } else {
                    updateFinger();
                }

            }
        });
        return titleBar;
    }

    public static EditFingerFragment newInstance() {
        return new EditFingerFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.edit_finger_fragment;
    }

    @Override
    protected void initViews() {
        Bundle arguments = getArguments();

        isAdd = arguments.getBoolean("isAdd", false);
        if (isAdd) {
            btnDeleteFinger.setVisibility(View.INVISIBLE);
            finger = new Finger();
        } else {
            finger = (Finger)arguments.getSerializable("finger");
            position = arguments.getInt("position");
            etFingerName.setText(finger.getFingerName());
        }
    }

    @Override
    protected void initListeners() {
        btnDeleteFinger.setOnClickListener(v -> {
            CustomRequest request = XHttp.custom();
            request.call(request.create(UserService.class).deleteFinger(finger))
                    .subscribeWith(new TipProgressLoadingSubscriber<ApiResult>(getProgressLoader()) {
                        @Override
                        protected void onSuccess(ApiResult apiResult) {
                            if (apiResult.isSuccess()) {
                                Utils.showToast("删除成功");
                                Intent intent = new Intent();
                                intent.putExtra("position", position);
                                intent.putExtra("isDelete", true);
                                setFragmentResult(500, intent);
                                popToBack();
                            } else {
                                Utils.showToast(apiResult.getMsg());
                            }

                        }
                    });
        });
    }

    /**
     * 更新指纹名称
     */
    private void updateFinger() {
        String fingerName = etFingerName.getText().toString().trim();
        if (Utils.isStringEmpty(fingerName)) {
            Utils.showToast("名称不能为空");
            return;
        }
        if (fingerName.equals(finger.getFingerName())) {
            return;
        }

        finger.setFingerName(fingerName);
        CustomRequest request = XHttp.custom();
        request.call(request.create(UserService.class).updateFinger(finger))
                .subscribeWith(new TipProgressLoadingSubscriber<ApiResult>(getProgressLoader()) {
                    @Override
                    protected void onSuccess(ApiResult apiResult) {
                        if (apiResult.isSuccess()) {
                            Utils.showToast("更新成功");
                            Intent intent = new Intent();
                            intent.putExtra("position", position);
                            intent.putExtra("name", fingerName);
                            setFragmentResult(500, intent);
                            popToBack();
                        } else {
                            Utils.showToast(apiResult.getMsg());
                        }

                    }
                });
    }

    private void addFinger() {
        String fingerName = etFingerName.getText().toString().trim();
        if (Utils.isStringEmpty(fingerName)) {
            Utils.showToast("名称不能为空");
            return;
        }

        finger.setUserId(Integer.parseInt(SPUtils.getInstance().getString("token", "-1")));
        finger.setFingerName(fingerName);

        CustomRequest request = XHttp.custom();
        request.call(request.create(UserService.class).addFinger(finger))
                .subscribeWith(new TipProgressLoadingSubscriber<ApiResult>(getProgressLoader()) {
                    @Override
                    protected void onSuccess(ApiResult apiResult) {
                        if (apiResult.isSuccess()) {
                            Utils.showToast("添加指纹中, 请录入指纹");
                            popToBack();
                        } else {
                            Utils.showToast(apiResult.getMsg());
                        }

                    }
                });


    }
}